#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <math.h>
#include <jasper/jasper.h>
#include "warpsol.h"


int main(int argc, char **argv) {
  jas_init();
  warpSol codec[2];
  //  pid_t pid = getpid();
  
  for(int i=1; i < argc; ++i) {
    codec[0].readSolexa(argv[i]);
  }
  codec[0].writeJasper("/tmp/test.jpc", "vcausal lazy nomct");
  codec[1].readJasper("/tmp/test.jpc");

  const int NX = codec[0].nx();
  const int NY = codec[0].ny();
  const int P  = codec[0].nPlane();
  assert(P  == argc - 1);
  assert(P  == codec[1].nPlane());
  assert(NX == codec[1].nx());
  assert(NY == codec[1].ny());

  for(int s=0; s < P; ++s) {
    float rms = 0;
    for(int x=0; x < NX; ++x) {
      for(int y=0; y < NY; ++y) {
	unsigned short int p1 = codec[0].getPixel(x,y,s);
	unsigned short int p2 = codec[1].getPixel(x,y,s);
	rms += (p1 - p2) * (p1 - p2);
      }
    }
    rms /= NX * NY;
    rms = sqrt(rms);

    std::cout << argv[s+1] << "\t" << rms << std::endl;
  }
 

  jas_cleanup();
}

